<?php
/**
 * Created by PhpStorm.
 * User: zhixuedong
 * Date: 2022/5/19
 * Time: 11:07
 */

namespace app\project\controller;


use app\common\service\EzuikitService;
use think\Controller;
use think\Db;
use think\Request;

class Monitor extends Controller
{
    //设备列表
    public function info(){
        $param          = Request::instance()->param();
        //获取所有项目
        $where['deviceSerial'] = ['neq',''];
        $where['pid'] = 0;
        $deviceName = "";
        if (isset($param["deviceName"]) && $param["deviceName"]){
            $where["title"] = ["like","%{$param["deviceName"]}%"];
            $deviceName = $param["deviceName"];
        }
        $list = Db::name("project_pro")->where($where)->select();

         $this->assign("deviceName",$deviceName);
        $this->assign("list",$list);
        return $this->fetch();
    }

    public function flv(){
        $id = Request::instance()->param("id");
        $detail = Db::name("project_pro")->where(["id" => $id])->find();
        if ($detail & empty($detail['img'])){
            try {
                $url = EzuikitService::capture($detail["deviceSerial"],$detail["channelNo"]);
                if ($url){
                    Db::name("project_pro")->where(["id" => $id])->update(['img'=>$url]);
                }
            }catch (\Exception $e){
                writeRuntimeLog("Monitor",$e->getMessage());
            }
        }

        $deviceSerial = explode(";",$detail['deviceSerial']);
        $channelNo = explode(";",$detail['channelNo']);
        $index = empty($_REQUEST['index'])?0:$_REQUEST['index'];
        $showDeviceSerial = $deviceSerial[$index];
        $showChannelNo = $channelNo[$index];

        $this->assign("deviceSerial",$deviceSerial);
        $this->assign("id",$id);

        $detail['flvAddress'] = "ezopen://open.ys7.com/".$showDeviceSerial."/".$showChannelNo.".live";
        $this->assign("detail",$detail);
        $this->assign("token",EzuikitService::getToken());
        return $this->fetch('flv');
    }


    public function detail(){
        $deviceSerial = Request::instance()->param("deviceSerial");
        $detail = Db::name("project_pro")->where(["deviceSerial" => $deviceSerial])->find();
       if(empty($detail)){
           $detail['title'] = "";
           $detail['flvAddress'] = "";
       }
        $detail['flvAddress'] = "ezopen://open.ys7.com/".$detail['deviceSerial']."/".$detail['channelNo'].".live";
        $this->assign("detail",$detail);
        $this->assign("token",EzuikitService::getToken());
        return $this->fetch('detail');
    }

}